package com.open.party.cloud.view.examine

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.jiangyy.easydialog.CommonDialog
import com.open.party.cloud.R
import com.open.party.cloud.model.CourseTeacherEntity
import com.open.party.cloud.model.TeacherRankEntity
import com.open.party.cloud.model.TeacherRankVO
import com.open.party.cloud.viewModel.ExamineViewModel
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.examine_class_teacher_rank_add.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ExamineClassTeacherRankActivity : AppEventBusTitleBaseActivity() {

    var courseTeacher: CourseTeacherEntity? = null
    var viewModel: ExamineViewModel? = null
    override fun getLayoutResId(): Int = R.layout.examine_class_teacher_rank_add

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<TeacherRankEntity>) {
        if (result.eventType != "getTeacherEval") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    showDataView(result.data)
                } else {
                    XUtils.toast().show(result.msg)
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    private fun showDataView(teacherRank: TeacherRankEntity) {
        if (teacherRank.isDiscipline == null || teacherRank.isDiscipline) {
            item001Rg.check(R.id.item001RgY)
            item01Et.visibility = View.GONE
        } else {
            item001Rg.check(R.id.item001RgN)
            item01Et.visibility = View.VISIBLE
            item01Et.setText(XUtils.string().getNotNullValue(teacherRank.disciplineDesc))
            item01Et.isEnabled = false
        }
        item001Rg.isEnabled = false
        item001RgY.isClickable = false
        item001RgN.isClickable = false

        item02Et.setText(teacherRank.teachAttitude.toString())
        item02Et.isEnabled = false

        item03Et.setText(teacherRank.teachContent.toString())
        item03Et.isEnabled = false

        item04Et.setText(teacherRank.teachFunction.toString())
        item04Et.isEnabled = false

        item05Et.setText(teacherRank.teachEffect.toString())
        item05Et.isEnabled = false

        item06Et.setText(teacherRank.description.toString())
        item06Et.isEnabled = false

        getTotalBtn.visibility = View.INVISIBLE
        totalScoreTv.text = teacherRank.totalScore.toString()
    }


    private fun initView(courseTeacher: CourseTeacherEntity) {
        if (courseTeacher.isEval) {
            setViewTitle("教师评估详情")
            viewModel!!.getTeacherEval(courseTeacher.classId, courseTeacher.id, courseTeacher.courseId)
        } else {
            setViewTitle("教师评估", "提交") {
                submit()
            }

            item001Rg.setOnCheckedChangeListener { _, checkedId ->
                when (checkedId) {
                    R.id.item001RgY -> {
                        discipline = true
                        item01Et.visibility = View.GONE
                    }
                    R.id.item001RgN -> {
                        discipline = false
                        item01Et.visibility = View.VISIBLE
                    }
                }
            }
            item001Rg.check(R.id.item001RgY)

//            getTotalBtn.setOnClickListener {
//                getTotal()
//            }

            initEditView()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        courseTeacher = intent.getSerializableExtra("courseTeacher") as CourseTeacherEntity
        viewModel = ExamineViewModel()

        initView(courseTeacher!!) //courseTeacher!!.isEval, courseTeacher!!.classId, courseTeacher!!.id
    }

    var discipline = false

    private fun submit() {
        val teacherRankVO = TeacherRankVO()
        teacherRankVO.courseId = courseTeacher!!.courseId
        teacherRankVO.classId = courseTeacher!!.classId

        if (XUtils.string().isEmpty(teacherRankVO.classId)) {
            XUtils.toast().show("班级id为空，请联系管理员")
            return
        }
        if (XUtils.string().isEmpty(teacherRankVO.courseId)) {
            XUtils.toast().show("课程id为空，请联系管理员")
            return
        }

        teacherRankVO.userId = courseTeacher!!.id
        teacherRankVO.userName = courseTeacher!!.userName
        teacherRankVO.isDiscipline = discipline.toString()
        if (!discipline) {
            val item01Val = item01Et.text.toString()
            if (XUtils.string().isEmpty(item01Val)) {
                XUtils.toast().show("第一项选否，请列举事例")
                return
            }

            teacherRankVO.disciplineDesc = item01Val
        }

        val item02Val = item02Et.text.toString()
        if (XUtils.string().isEmpty(item02Val)) {
            XUtils.toast().show("请填写教学态度评分")
            return
        } else {
            val v02 = item02Val.toInt()
            if (v02 > 20 || v02 < 0) {
                XUtils.toast().show("教学态度评分应小于等于20且大于0")
                return
            }
            teacherRankVO.teachAttitude = v02
        }

        val item03Val = item03Et.text.toString()
        if (XUtils.string().isEmpty(item03Val)) {
            XUtils.toast().show("请填写教学内容评分")
            return
        } else {
            val v03 = item03Val.toInt()
            if (v03 > 30 || v03 < 0) {
                XUtils.toast().show("教学内容评分应小于等于30且大于0")
                return
            }
            teacherRankVO.teachContent = v03
        }

        val item04Val = item04Et.text.toString()
        if (XUtils.string().isEmpty(item04Val)) {
            XUtils.toast().show("请填写教学方法评分")
            return
        } else {
            val v04 = item04Val.toInt()
            if (v04 > 20 || v04 < 0) {
                XUtils.toast().show("教学方法评分应小于等于20且大于0")
                return
            }
            teacherRankVO.teachFunction = v04
        }

        val item05Val = item05Et.text.toString()
        if (XUtils.string().isEmpty(item05Val)) {
            XUtils.toast().show("请填写教学效果评分")
            return
        } else {
            val v05 = item03Val.toInt()
            if (v05 > 30 || v05 < 0) {
                XUtils.toast().show("教学效果评分应小于等于30且大于0")
                return
            }
            teacherRankVO.teachEffect = v05
        }

        val item06Val = item06Et.text.toString()
        if (XUtils.string().isEmpty(item06Val)) {
            XUtils.toast().show("请输入所需反馈的内容")
            return
        } else {
            teacherRankVO.description = item06Val
        }

        teacherRankVO.totalScore = teacherRankVO.teachAttitude + teacherRankVO.teachContent + teacherRankVO.teachFunction + teacherRankVO.teachEffect;

        CommonDialog.Builder(this)
            .setCancelable(false)
            .setTitle("重要提示")
            .setMessage("评价后不可更改，确定提交吗?")
            .setPositiveButton("提交") {
                showLoadingDialog("正在提交")
                viewModel!!.schoolTeacherEvalSave(teacherRankVO)
            }.setNegativeButton("取消") {
            }.show()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun addCallback(result: ResultInfo<TeacherRankEntity>) {
        if (result.eventType != "schoolTeacherEvalSave") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    XUtils.toast().showLong("评价已提交")
                    finish()
                } else {
                    XUtils.toast().show(result.msg)
                }
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    private fun getTotal() {
        val item02Val = item02Et.text.toString()
        var v2 = 0
        v2 = if (XUtils.string().isNotEmpty(item02Val)) {
            val v02 = item02Val.toInt()
            if (v02 > 20 || v02 < 0) {
                XUtils.toast().show("教学态度评分应小于等于20且大于0")
                item02Et.setText("")
                XUtils.view().focusMoveToEnd(item02Et)
                return
            }
            item02Val.toInt()

        } else {
            0
        }

        val item03Val = item03Et.text.toString()
        var v3 = 0
        v3 = if (XUtils.string().isNotEmpty(item03Val)) {
            val v03 = item03Val.toInt()
            if (v03 > 30 || v03 < 0) {
                XUtils.toast().show("教学内容评分应小于等于30且大于0")
                item03Et.setText("")
                XUtils.view().focusMoveToEnd(item03Et)
                return
            }
            item03Val.toInt()

        } else {
            0
        }

        val item04Val = item04Et.text.toString()
        var v4 = 0
        v4 = if (XUtils.string().isNotEmpty(item04Val)) {
            val v04 = item04Val.toInt()
            if (v04 > 20 || v04 < 0) {
                XUtils.toast().show("教学方法评分应小于等于20且大于0")
                item04Et.setText("")
                XUtils.view().focusMoveToEnd(item04Et)
                return
            }
            item04Val.toInt()
        } else {
            0
        }

        val item05Val = item05Et.text.toString()
        val v5 = if (XUtils.string().isNotEmpty(item05Val)) {
            val v05 = item05Val.toInt()
            if (v05 > 30 || v05 < 0) {
                XUtils.toast().show("教学效果评分应小于等于30且大于0")
                item05Et.setText("")
                XUtils.view().focusMoveToEnd(item05Et)
                return
            }
            item05Val.toInt()
        } else {
            0
        }

        totalScoreTv.text = (v2 + v3 + v4 + v5).toString()
    }

    private fun initEditView() {

        val textWatcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                getTotal()
            }
        };

        item02Et.addTextChangedListener(textWatcher)
        item03Et.addTextChangedListener(textWatcher)
        item04Et.addTextChangedListener(textWatcher)
        item05Et.addTextChangedListener(textWatcher)
    }
}